﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DAL;

namespace BLL
{
  public class CategoryBLL
  {
    //Get all

    public List<Category> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
         return data.Categories.OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Category> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get By Parent

    public List<Category> GetByParent(int idParent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.Parent == idParent).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Category> GetByParent(int idParent, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.Parent == idParent && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Category> GetByParent(int idParent, int top)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Categories.Where(p => p.Parent == idParent).Take(top).OrderBy(p => p.Sort).ToList();
        }
    }

    //Get by Level

    public List<Category> GetByLevel(int level)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.Level == level).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Category> GetByLevel(int level, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.Level == level && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get by Page
    public List<Category> GetByPage(List<Category> list, int page, int size)
    {
      page--;
      int skip = page * size;
      return list.Skip(skip).Take(size).ToList();
    }

    //Get by id
    public Category GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public Category GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Categories.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Get max sort

    public byte GetMaxSort(int idParent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Category obj = data.Categories.Where(p => p.Parent == idParent).OrderByDescending(p => p.Sort).FirstOrDefault();
        if (obj != null)
          return (byte)(obj.Sort + 1);
        else
          return 1;
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Category objFirst = data.Categories.Where(p => p.Id == idFirst).FirstOrDefault();
        Category objSecond = data.Categories.Where(p => p.Id == idSecond).FirstOrDefault();

        byte tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }

    public bool IsHasChild(int id)
    {
      if (GetByParent(id).Count == 0)
        return false;
      else return true;
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Category obj = data.Categories.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }
    public void Enable(int id,bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Category obj = data.Categories.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Add

    public void Add(Category obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Categories.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update

    public void Update(Category obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Category category = data.Categories.Where(p => p.Id == obj.Id).FirstOrDefault();

        category.CreateDate = obj.CreateDate;
        category.Image = obj.Image;
        category.isEnable = obj.isEnable;
        category.Level = obj.Level;
        category.Name = obj.Name;
        category.Name_En = obj.Name_En;
        category.Description = obj.Description;
        category.Content = obj.Content;
        category.Parent = obj.Parent;
        category.Sort = obj.Sort;
        category.isDelete = obj.isDelete;        

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Category obj = data.Categories.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.Categories.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
